//
//  ForgivePasswordViewController.m
//  Lucky28
//
//  Created by zr on 2018/8/1.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "ForgivePasswordViewController.h"

@interface ForgivePasswordViewController ()

@property (nonatomic, weak)UITextField * accountText;
@property (nonatomic, weak)UITextField * passwordText;
@property (nonatomic, weak)UITextField * verifyText;
@property (nonatomic, weak)UITextField * repasswordText;
@property (nonatomic, weak)UIButton * loginButton;
@property (nonatomic, weak)UIButton * verifyButton;

//计时器
@property (nonatomic, strong)NSTimer * countDownTimer;

@end

//倒计时总的秒数
static NSInteger  secondsCountDown = 60;


@implementation ForgivePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = MAINCOLOR;
    
    [self setupNav];
    [self setupUI];

}


- (void)setupNav{
    
    self.title = @"忘记密码";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(0, 0, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}



- (void)setupUI{
    
    UITextField * accountText = [[UITextField alloc] init];
    
    accountText.borderStyle = UITextBorderStyleNone;
    accountText.backgroundColor = [UIColor clearColor];
    //设置光标颜色
    accountText.tintColor = [UIColor whiteColor];
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    accountText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入您的手机号" attributes:attrs];
    accountText.textColor = [UIColor whiteColor];
    accountText.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
    //设置显示模式为永远显示(默认不显示)
    accountText.leftViewMode = UITextFieldViewModeAlways;
    accountText.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    UIView * rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35 * SCREENH6, 20 * SCREENH6)];
    UIImageView *image=[[UIImageView alloc] initWithFrame:CGRectMake(5, 0, 16 * SCREENH6, 17.5 * SCREENH6)];//WithImage:[UIImage imageNamed:@"手机号"]];
    image.image = ImageNamed(@"Icon");
    [rightView addSubview:image];
    accountText.leftView = rightView;
    accountText.leftViewMode = UITextFieldViewModeAlways;
        accountText.keyboardType = UIKeyboardTypeNumberPad;
    //    accountText.keyboardAppearance = UIKeyboardAppearanceDark;
    
    self.accountText = accountText;
    
    [self.view addSubview:self.accountText];
    
    
    
    UITextField * passwordText = [[UITextField alloc] init];
    
    passwordText.borderStyle = UITextBorderStyleNone;
    passwordText.secureTextEntry = YES;
    passwordText.backgroundColor = [UIColor clearColor];
    //    passwordText.delegate = self;
    //设置光标颜色
    passwordText.tintColor = [UIColor whiteColor];
    //修改占位符文字颜色
    passwordText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入您的新密码" attributes:attrs];
    passwordText.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    passwordText.textColor = [UIColor whiteColor];
    passwordText.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
    //设置显示模式为永远显示(默认不显示)
    passwordText.leftViewMode = UITextFieldViewModeAlways;
    //    accountText.font = [UIFont systemFontOfSize:15];
    UIView * leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35 * SCREENH6, 20 * SCREENH6)];
    UIImageView *image1=[[UIImageView alloc] initWithFrame:CGRectMake(5, 0, 15 * SCREENH6, 19 * SCREENH6)];//WithImage:[UIImage imageNamed:@"手机号"]];
    image1.image = ImageNamed(@"Lock");
    [leftView addSubview:image1];
    passwordText.leftView = leftView;
    passwordText.leftViewMode = UITextFieldViewModeAlways;
    //    passwordText.keyboardType = UIKeyboardTypeDefault;
    //    passwordText.keyboardAppearance = UIKeyboardAppearanceDark;
    
    self.passwordText = passwordText;
    
    [self.view addSubview:self.passwordText];
    
    
    UITextField * verifyText = [[UITextField alloc] init];
    
    verifyText.borderStyle = UITextBorderStyleNone;
    verifyText.backgroundColor = [UIColor clearColor];
    //    passwordText.delegate = self;
    //设置光标颜色
    verifyText.tintColor = [UIColor whiteColor];
    //修改占位符文字颜色
    verifyText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入验证码" attributes:attrs];
    verifyText.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    verifyText.textColor = [UIColor whiteColor];
    verifyText.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
    //设置显示模式为永远显示(默认不显示)
    verifyText.leftViewMode = UITextFieldViewModeAlways;
    //    accountText.font = [UIFont systemFontOfSize:15];
    UIView * leftView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35 * SCREENH6, 20 * SCREENH6)];
    UIImageView *image2=[[UIImageView alloc] initWithFrame:CGRectMake(5, 0, 15 * SCREENH6, 19 * SCREENH6)];//WithImage:[UIImage imageNamed:@"手机号"]];
    image2.image = ImageNamed(@"yanzhengma");
    [leftView1 addSubview:image2];
    verifyText.leftView = leftView1;
    verifyText.leftViewMode = UITextFieldViewModeAlways;
    ;verifyText.keyboardType = UIKeyboardTypeNumberPad;
    //    passwordText.keyboardAppearance = UIKeyboardAppearanceDark;
    
    self.verifyText = verifyText;
    
    [self.view addSubview:self.verifyText];
    
    
    UITextField * repasswordText = [[UITextField alloc] init];
    
    repasswordText.borderStyle = UITextBorderStyleNone;
    repasswordText.secureTextEntry = YES;
    repasswordText.backgroundColor = [UIColor clearColor];
    //    passwordText.delegate = self;
    //设置光标颜色
    repasswordText.tintColor = [UIColor whiteColor];
    //修改占位符文字颜色
    repasswordText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请再输入一次" attributes:attrs];
    repasswordText.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    repasswordText.textColor = [UIColor whiteColor];
    repasswordText.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
    //设置显示模式为永远显示(默认不显示)
    repasswordText.leftViewMode = UITextFieldViewModeAlways;
    //    accountText.font = [UIFont systemFontOfSize:15];
    UIView * leftView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35 * SCREENH6, 20 * SCREENH6)];
    UIImageView *image3=[[UIImageView alloc] initWithFrame:CGRectMake(5, 0, 15 * SCREENH6, 19 * SCREENH6)];//WithImage:[UIImage imageNamed:@"手机号"]];
    image3.image = ImageNamed(@"Lock");
    [leftView2 addSubview:image3];
    repasswordText.leftView = leftView2;
    repasswordText.leftViewMode = UITextFieldViewModeAlways;
    //    passwordText.keyboardType = UIKeyboardTypeDefault;
    //    passwordText.keyboardAppearance = UIKeyboardAppearanceDark;
    
    self.repasswordText = repasswordText;
    
    [self.view addSubview:self.repasswordText];
    
    
    UIButton * verifyButton = [[UIButton alloc] init];
    
    [verifyButton setTitle:@"   发送验证码   " forState:UIControlStateNormal];
    verifyButton.titleLabel.font = [UIFont boldSystemFontOfSize:15 * SCREENH6];
    [verifyButton setBackgroundColor:[UIColor whiteColor]];
    [verifyButton setTitleColor:MAINCOLOR forState:UIControlStateNormal];
    [verifyButton addTarget:self action:@selector(didSentVerButton) forControlEvents:UIControlEventTouchUpInside];
    //处理圆角
    verifyButton.layer.cornerRadius = 10.0f;
    
    
    self.verifyButton = verifyButton;
    
    [self.view addSubview:self.verifyButton];
    
    
    UIButton * loginButton = [[UIButton alloc] init];
    
    [loginButton setTitle:@"提交" forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont boldSystemFontOfSize:16 * SCREENH6];
    [loginButton setBackgroundImage:[UIImage imageNamed:@"Button"] forState:UIControlStateNormal];
    
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(didLoginButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.loginButton = loginButton;
    
    [self.view addSubview:self.loginButton];
    
}


- (void)setupDownTimer{
    
    _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownAction) userInfo:nil repeats:YES];
}

//定时器的方法
- (void)countDownAction{
    
    secondsCountDown --;
    
    [self.verifyButton setTitle:[NSString stringWithFormat:@"  %lds后获取  ",(long)secondsCountDown] forState:UIControlStateDisabled];
    
    [self.verifyButton setBackgroundColor:[UIColor lightGrayColor]];
    
    //当倒计时到0时做需要的操作，button变成可选状态
    if(secondsCountDown == 0){
        
        [_countDownTimer invalidate];
        
        [self.verifyButton setBackgroundColor:[UIColor whiteColor]];
        [self.verifyButton setTitle:@"  获取验证码  " forState:UIControlStateNormal];
        self.verifyButton.enabled = YES;
    }
}



- (void)viewDidLayoutSubviews{
    
    [self.accountText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(35 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-35 * SCREENH6);
        make.height.mas_equalTo(50 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(35 * SCREENH6);
    }];
    
    [self.verifyText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.height.mas_equalTo(self.passwordText);
        make.width.mas_equalTo(150 * SCREENH6);
        make.top.mas_equalTo(self.accountText.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.verifyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.accountText.mas_trailing);
        make.bottom.mas_equalTo(self.verifyText.mas_bottom).offset(-5 * SCREENH6);
        make.height.mas_equalTo(30 * SCREENH6);
    }];
    
    [self.passwordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.accountText);
        make.top.mas_equalTo(self.verifyText.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.repasswordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.passwordText);
        make.top.mas_equalTo(self.passwordText.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(45 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-45 * SCREENH6);
        make.height.mas_equalTo(50 * SCREENH6);
        make.top.mas_equalTo(self.repasswordText.mas_bottom).offset(10 * SCREENH6);
    }];
   
}


- (void)didLoginButton{
    
    [self.accountText resignFirstResponder];
    [self.verifyText resignFirstResponder];
    [self.passwordText resignFirstResponder];
    [self.repasswordText resignFirstResponder];
    
    if ([self.accountText.text isEqualToString:@""] || [self.verifyText.text isEqualToString:@""] || [self.passwordText.text isEqualToString:@""] || [self.repasswordText.text isEqualToString:@""]) {
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"请完善注册信息！"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
                                                                  
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
        
    }else{
        
        SHOWHUD;
        
        NSDictionary * parametersDic = @{@"mobile":self.accountText.text,@"password":self.repasswordText.text,@"code":self.verifyText.text};
        
        [[NetworkTool sharedTools] POST:@"/index/Member/forgetpwd" parameters:parametersDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
            
            HIDDENHUD;
            
            NSLog(@"res===%@",responseObject);
            
            NSString * codeString = [NSString stringWithFormat:@"%@",responseObject[@"status"]];
            
            if ([codeString isEqualToString:@"200"]) {
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:@"恭喜你修改成功！赶快去登录吧！"
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                          [self.navigationController popViewControllerAnimated:YES];
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                
            }else{
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseObject[@"msg"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            HIDDENHUD;
            
            NSLog(@"error===%@",error);
        }];
        
    }
}

- (void)didSentVerButton{
    
    self.verifyButton.enabled = NO;
    secondsCountDown = 60;
    
    [self setupDownTimer];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.accountText resignFirstResponder];
    [self.passwordText resignFirstResponder];
    [self.verifyText resignFirstResponder];
    [self.repasswordText resignFirstResponder];
}

- (void)backToView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
